/*
* $Id: InfoItem.cpp 122 2010-07-22 17:05:50Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "InfoItem.h"

#define SIZE_BUTTON_MORE	100
#define SIZE_IMAGE_Y		46
#define SIZE_IMAGE_X		31

CItem::CItem(const std::tstring& strContent, const std::tstring& strFontName,
			int nFontSize, bool bBold, bool bItalic)
: InfoItem(strContent, strFontName, nFontSize, bBold, bItalic), s_size(0, 0){}
CSize CItem::InitSize(int cx){
	CFont* pFont = CCommonFunction::GetFont(s_strFontName, s_nFontSize, s_bBold, s_bItalic);
	s_size = CCommonFunction::GetSizeYText(s_strContent, cx - 5, pFont);
	pFont->DeleteObject();
	delete pFont;
	return s_size;
}
CSize CItem::GetSize(){
	return s_size;
}


CInfoItem::CInfoItem(int nId, IDrawer* pDrawer)
: m_buttonMore(IApplication::GetApp()->GetLanguageManager()->GetString(MORE_ID), pDrawer),
m_point(0, 0), m_bSelected(false), m_nId(nId){
	m_buttonMore.SetCx(SIZE_BUTTON_MORE);
}
CInfoItem::~CInfoItem(){}

void CInfoItem::SetPosition(CPoint pos){
	m_point = pos;
	m_buttonMore.SetPos(CPoint(m_point.x + m_size.cx - SIZE_BUTTON_MORE, m_point.y));
}

CSize CInfoItem::GetSize(){
	return m_size;
}

void CInfoItem::Paint(CDC* dc){
	CBrush brush, brush1;
	brush1.CreateSolidBrush(RGB(200, 200, 200));
	brush.CreateSolidBrush(RGB(255, 255, 255));
	if(m_bSelected)
		dc->FillRect(CRect(CPoint(m_point.x + SIZE_IMAGE_X, m_point.y), m_size), &brush1);
	else
		dc->FillRect(CRect(CPoint(m_point.x + SIZE_IMAGE_X, m_point.y), m_size), &brush);
	dc->FillRect(CRect(CPoint(m_point.x, m_point.y + SIZE_IMAGE_Y), CSize(SIZE_IMAGE_X, m_size.cy - SIZE_IMAGE_Y)), &brush);
	brush.DeleteObject();
	brush1.DeleteObject();
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_BF);
	dc->DrawState(m_point, CSize(SIZE_IMAGE_X, SIZE_IMAGE_Y), &bitmap, DST_BITMAP, NULL);
	bitmap.DeleteObject();
	dc->MoveTo(m_point);
	dc->LineTo(m_point.x + m_size.cx - 1, m_point.y);
	dc->LineTo(m_point.x + m_size.cx - 1, m_point.y + m_size.cy);
	dc->LineTo(m_point.x, m_point.y + m_size.cy);
	dc->LineTo(m_point);
	dc->SetBkMode(TRANSPARENT);
	int pointY = m_point.y + 2;
	for(std::vector<CItem>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		CFont* pFont = CCommonFunction::GetFont(it->s_strFontName, it->s_nFontSize, it->s_bBold, it->s_bItalic);
		HGDIOBJ oldFont = dc->SelectObject(pFont->m_hObject);
		CRect rect(CPoint(m_point.x + SIZE_IMAGE_X + 5, pointY), it->GetSize());
		dc->DrawText(it->s_strContent.c_str(), (int)it->s_strContent.size(), rect, DT_LEFT | DT_WORDBREAK);
		dc->SelectObject(oldFont);
		pFont->DeleteObject();
		delete pFont;
		pointY += it->GetSize().cy;
	}
	dc->SetBkMode(BKMODE_LAST);
	if(m_bPaintButton)
		m_buttonMore.Paint(dc);
}

void CInfoItem::SetSizeCx(int cx){
	m_size.SetSize(cx, 0);
	for(std::vector<CItem>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		m_size.cy += it->InitSize(cx - SIZE_BUTTON_MORE - SIZE_IMAGE_X).cy;
	m_buttonMore.SetPos(CPoint(m_point.x + m_size.cx - SIZE_BUTTON_MORE, m_point.y));
	m_buttonMore.SetCy(m_size.cy);
	if(m_size.cy < SIZE_IMAGE_Y)
		m_size.cy = SIZE_IMAGE_Y;
	m_size.cy += 5;
}

void CInfoItem::ShowButton(bool bShow){
	m_bPaintButton = bShow;
}

void CInfoItem::AddInfoItem(InfoItem item){
	m_aItems.push_back(CItem(item.s_strContent, item.s_strFontName, item.s_nFontSize, item.s_bBold, item.s_bItalic));
}

bool CInfoItem::ProcessMouse(unsigned int nButton, CPoint pos, int param){
	if(nButton == WM_MOUSEMOVE && !m_buttonMore.GetRect().PtInRect(pos)){
		return m_buttonMore.ProcessMouse(WM_MOUSELEAVE, pos, param);
	} else if(m_buttonMore.ProcessMouse(nButton, pos, param)){
		return true;
	}
	return false;
}

bool CInfoItem::IsSelect(){
	return m_bSelected;
}

void CInfoItem::SetSelect(bool val){
	m_bSelected = val;
}

int CInfoItem::GetId(){
	return m_nId;
}

